<?php

/**
 *
 */
class PageController extends MController {

    /**
     * 
     */
    public function indexAction() {
        $pages = Page::all(array('order'=>'id DESC'));

        $pagesTable = new MHtmlTable($pages, array('created_at', 'updated_at'));
        $buttons = array(
            'update'=>array('title'=>'Mise à jour', 'href'=>__route('ml-admin/page/update',array('id'=>'{id}')), 'content'=>'Update'),
            'delete'=>array('title'=>'Supprimer', 'href'=>  __route('ml-admin/page/delete', array('id'=>'{id}')), 'content'=>'Supprimer')
        );
        $pagesTable->addColumn(new MHtmlButtonColumn($pagesTable,$buttons));

        $this->render('index', array(
            'pages'=>$pages,
            'pagesTable'=>$pagesTable,
        ));
    }


    /**
     *
     */
     public function createAction() {

        // User is connected ?
        if(MSession::getInstance()->IsLogged()) {

            // Render the form
            if( !$this->getRequest()->isPost() ) {
                $createPageForm = new CreatePageForm();
                $this->render('create', array('createPageForm' => $createPageForm));
            }
            // Create the page
            else {
                $postDatas = $this->getRequest()->getPost();

                try {
                        Page::create(array(
                                            'title' => $postDatas['title'],
                                            'html_content' => $postDatas['html_content'],
                                            'created_at' => date('Y-m-d H:i:s'),
                                            'updated_at' => date('Y-m-d H:i:s'),
                                            'activated' => true,
                                            'author' => MSession::getInstance()->getValue('userID'),
                        ));

                        __redirect(__route('ml-admin/page'));
                } catch(Exception $e) {
                    // TODO catch the exception
                }
            }
        }
        // User is not logged
        else {
            __redirect(__route('ml-admin/'));
        }
     }


     /**
      *
      */
     public function deleteAction() {
         // User is connected ?
        if(MSession::getInstance()->IsLogged()) {
            $getDatas = $this->getRequest()->getGet();
            $page = Page::find($getDatas['id']);
            $page->delete();
            __redirect(__route('ml-admin/page'));
        } else {
            __redirect(__route('ml-admin'));
        }
     }


     /**
      * 
      */
     public function updateAction() {
         // User is connected ?
        if(MSession::getInstance()->IsLogged()) {
            $getDatas = $this->getRequest()->getGet();
            $page = Page::find($getDatas['id']);

            $createPageForm = new CreatePageForm();
            $createPageForm->getElement('title')->setValue($page->title);
            $createPageForm->getElement('html_content')->setValue($page->html_content);
            $createPageForm->getElement('create')->setValue('Update');

            $this->render('create', array('createPageForm' => $createPageForm));
        } else {
            __redirect(__route('ml-admin'));
        }
     }
}

?>
